<template>
  <div class="index">
    <el-container>
      <el-header>
        <div class="logo">
          <img src="../../assets/new_logo.png" alt="">
        </div>
        <div class="user">
          <el-avatar :size="50" :src="$store.getters.getUser.avatarUrl"></el-avatar>
          <el-link @click="logout" type="danger">注销</el-link>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu class="menu">
            <el-submenu :index="index" v-for="(item ,index) in menus" :key="index">
              <template slot="title"><i class="el-icon-message"></i>{{ item.righttext }}</template>
              <el-menu-item v-for="(citem,cindex) in item.children" :key="cindex" @click="move(citem.righturl)">
                {{ citem.righttext }}
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <!--添加了一下这一行代码之后，组件将会在页面显示-->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import store from '../../store'
import axios from '../../utils/request'

export default {
  name: 'Index',
  data () {
    return {
      menus: []
    }
  },
  created () {
    this.initMenu()
  },
  beforeRouteEnter (to, from, next) {
    if (store.getters.getUser.id) {
      return next()
    }
    next('/login')
  },
  methods: {
    logout () {
      this.$store.dispatch('asyncUpdateUser', {})
      this.$router.push('/login')
    },
    initMenu () {
      // 根据角色查询出这个角色的权限菜单
      axios({
        method: 'get',
        url: '/admin/role/menus'
      }).then(jsonData => {
        console.log(jsonData)
        this.menus = jsonData.data
      })
    },
    move (url) {
      // alert(url)
      this.$router.push(url)
    }
  }
}
</script>

<style scoped>

.index{
  height: 500px;
}

.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
  display: flex;
}

.logo {
  flex: 1;
  text-align: left;
}

.user {
  flex: 1;
  text-align: right;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  /*line-height: 160px;*/
}

body > .el-container {
  margin-bottom: 40px;
}
.menu{
  height: 500px;
}
</style>
